﻿@using System.Reflection
@using System.Linq.Expressions
@using System.ComponentModel.DataAnnotations
@
@using Whoever.Framework.Base
@using Whoever.Framework.Attributes
@using Whoever.Patterns.Website2.Base
@model Whoever.Patterns.Website2.Base.PresentationModel
@{
    Type baseProperty = Model.ViewModel.Entity.GetType();
}
@{
    ViewBag.Title = string.Format("{0}", Model.ViewModel.EntityDisplayName);
}

<div class="page-header">
    <h1>@Model.ViewModel.EntityDisplayName</h1>
</div>

<div class="btn-toolbar">
    <div class="btn-group">
        @Html.ActionLink(string.Format("Agregar {0}", Model.ViewModel.EntityDisplayName), "Create", null, new { @class = "btn btn-primary" })
    </div>
</div>

@using (Html.BeginForm("Index", baseProperty.Name, FormMethod.Get, new { @class = "form-horizontal" }))
{    
    <fieldset>
        <legend>Buscar por @Model.ViewModel.EntityDisplayName</legend>
        @foreach (var property in Model.ViewModel.GetType().GetProperties().Where(p => !p.GetGetMethod().IsVirtual && p.GetCustomAttributes(typeof(FilterField), true).Count() > 0 && p.GetCustomAttributes(typeof(IdentityField), true).Count() == 0 && p.GetCustomAttributes(typeof(ForeignKeyField), true).Count() == 0))
        {
            @Html.Partial("_FormFilter", new FieldModel { ViewModel = Model.ViewModel, Property = property });
        }
        @foreach (var property in Model.ViewModel.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(SelectableRelationship), true).Length != 0))
        {
            SelectableRelationship attribute = property.GetCustomAttributes(typeof(SelectableRelationship), true).First() as SelectableRelationship;
            @Html.Partial("_FormCombo", new ComboModel { ViewModel = Model.ViewModel, PropertyList = property, PropertyForeignKey = Model.ViewModel.Entity.GetType().GetProperty(attribute.ForeignKeyName), PropertyListKey = attribute.RelationKeyName, PropertyListValue = attribute.RelationValueName });
        }
        <div class="form-buttons">
            <input type="submit" value="Buscar" class="btn btn-primary btn-large" id="btnUpdate" />
        </div>
    </fieldset>
}

@Html.Partial("_Grid", Model.GridModel)


